﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.OracleClient;
using PPAP.Model;
using System.Data;

namespace PPAP.DAO
{
    public class SetLevelDAO
    {
        public SetLevelDAO() { }
        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(SetLevel model)
        {
            model.ID = DbHelperOra.GetMaxID("ID", "SETLEVEL");
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into SETLEVEL(");
            strSql.Append("ID,PPARID,NO,CREATETIME,CREATEUSER,TYPE)");
            strSql.Append(" values (");
            strSql.Append(":ID,:PPARID,:NO,:CREATETIME,:CREATEUSER,:TYPE)");
            OracleParameter[] parameters = {
					new OracleParameter(":ID", OracleType.Number,4),
					new OracleParameter(":PPARID", OracleType.Number,4),
					new OracleParameter(":NO", OracleType.Number,4),
					new OracleParameter(":CREATETIME", OracleType.DateTime),
					new OracleParameter(":CREATEUSER", OracleType.VarChar,150),
					new OracleParameter(":TYPE", OracleType.Number,4)};
            parameters[0].Value = model.ID;
            parameters[1].Value = model.PPARID;
            parameters[2].Value = model.NO;
            parameters[3].Value = model.CREATETIME;
            parameters[4].Value = model.CREATEUSER;
            parameters[5].Value = model.TYPE;

           return DbHelperOra.ExecuteSql(strSql.ToString(), parameters)>0?int.Parse(model.ID.ToString()):0;
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public int Update(SetLevel model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update SETLEVEL set ");
            strSql.Append("ID=:ID,");
            strSql.Append("PPARID=:PPARID,");
            strSql.Append("NO=:NO,");
            strSql.Append("CREATETIME=:CREATETIME,");
            strSql.Append("CREATEUSER=:CREATEUSER,");
            strSql.Append("TYPE=:TYPE");
            strSql.Append(" where id="+model.ID);
            OracleParameter[] parameters = {
					new OracleParameter(":ID", OracleType.Number,4),
					new OracleParameter(":PPARID", OracleType.Number,4),
					new OracleParameter(":NO", OracleType.Number,4),
					new OracleParameter(":CREATETIME", OracleType.DateTime),
					new OracleParameter(":CREATEUSER", OracleType.VarChar,150),
					new OracleParameter(":TYPE", OracleType.Number,4)};
            parameters[0].Value = model.ID;
            parameters[1].Value = model.PPARID;
            parameters[2].Value = model.NO;
            parameters[3].Value = model.CREATETIME;
            parameters[4].Value = model.CREATEUSER;
            parameters[5].Value = model.TYPE;

            return DbHelperOra.ExecuteSql(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 删除一条数据   删除一个ppar所以记录
        /// </summary>
        public int Delete(int pparid)
        {
            //该表无主键信息，请自定义主键/条件字段
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from SETLEVEL ");
            strSql.Append(" where  pparid="+pparid);
            OracleParameter[] parameters = {
};

           return DbHelperOra.ExecuteSql(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public SetLevel GetModel(int id)
        {
            //该表无主键信息，请自定义主键/条件字段
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,PPARID,NO,CREATETIME,CREATEUSER,TYPE from SETLEVEL ");
            strSql.Append(" where id="+id);
            OracleParameter[] parameters = {
};

            SetLevel model = new SetLevel();
            DataSet ds = DbHelperOra.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["ID"].ToString() != "")
                {
                    model.ID = int.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["PPARID"].ToString() != "")
                {
                    model.PPARID = int.Parse(ds.Tables[0].Rows[0]["PPARID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["NO"].ToString() != "")
                {
                    model.NO = int.Parse(ds.Tables[0].Rows[0]["NO"].ToString());
                }
                if (ds.Tables[0].Rows[0]["CREATETIME"].ToString() != "")
                {
                    model.CREATETIME = DateTime.Parse(ds.Tables[0].Rows[0]["CREATETIME"].ToString());
                }
                model.CREATEUSER = ds.Tables[0].Rows[0]["CREATEUSER"].ToString();
                if (ds.Tables[0].Rows[0]["TYPE"].ToString() != "")
                {
                    model.TYPE = int.Parse(ds.Tables[0].Rows[0]["TYPE"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            //strSql.Append("select app.ID,PPARID,NO,app.CREATETIME,CREATEUSER,a.sx  FROM setlevel app left join attachmenttype a on app.no=a.id  ");
            strSql.Append(@"select app.ID,PPARID,NO,app.CREATETIME,CREATEUSER,a.sx,t.rid  FROM setlevel app left join attachmenttype a on app.no=a.id 
left join(select t.*,rownum rid from(select p.*,t.bdnttemplateavailable,t.uploadedfile from ATTACHMENTTYPE p left join ATTACHMENTTEMPLATE t on p.id=t.attachmenttypeid where    p.xs=1 order by sx) t)t
on a.id=t.id  ");
            //strSql.Append(" FROM SETLEVEL ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperOra.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取PPAR Level配置
        /// </summary>
        /// <param name="pid"></param>
        /// <returns></returns>
        public List<SetLevel> GetListForPid(string pid)
        {
            string sql = "select * from setlevel where pparid="+pid;
            List<SetLevel> list = new List<SetLevel>();
            DataSet ds = DbHelperOra.Query(sql);
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                SetLevel model = new SetLevel();
                if (dr["ID"].ToString() != "")
                {
                    model.ID = int.Parse(dr["ID"].ToString());
                }
                if (dr["PPARID"].ToString() != "")
                {
                    model.PPARID = int.Parse(dr["PPARID"].ToString());
                }
                if (dr["NO"].ToString() != "")
                {
                    model.NO = int.Parse(dr["NO"].ToString());
                }
                if (dr["CREATETIME"].ToString() != "")
                {
                    model.CREATETIME = DateTime.Parse(dr["CREATETIME"].ToString());
                }
                model.CREATEUSER = dr["CREATEUSER"].ToString();
                if (dr["TYPE"].ToString() != "")
                {
                    model.TYPE = int.Parse(dr["TYPE"].ToString());
                }
                list.Add(model);
            }
            return list;
        }
    }
}
